# 示例：Python 3.12 + CUDA 12.4（PyTorch 官方支持的最新组合）
FROM nvidia/cuda:12.4.1-devel-ubuntu22.04

# 安装 Python 3.12
ENV DEBIAN_FRONTEND=noninteractive

# 备份原始源文件并配置阿里云镜像源 
# RUN cp /etc/apt/sources.list /etc/apt/sources.list.bak && \
#     sed -i "s@http://.*archive.ubuntu.com@https://mirrors.aliyun.com@g" /etc/apt/sources.list && \
#     sed -i "s@http://.*security.ubuntu.com@https://mirrors.aliyun.com@g" /etc/apt/sources.list

# 安装编译依赖
RUN apt-get update && apt-get install -y \
    build-essential \
    zlib1g-dev \
    libncurses5-dev \
    libgdbm-dev \
    libnss3-dev \
    libssl-dev \
    libreadline-dev \
    libffi-dev \
    libbz2-dev \
    libncursesw5-dev \
    libsqlite3-dev \
    libexpat1-dev \
    liblzma-dev \
    tk-dev \
    wget \
    curl \
    && rm -rf /var/lib/apt/lists/*

# 下载并编译安装Python 3.12
RUN cd /tmp && \
    wget https://mirrors.huaweicloud.com/python/3.12.0/Python-3.12.0.tar.xz && \
    tar -xf Python-3.12.0.tar.xz && \
    cd Python-3.12.0 && \
    ./configure --enable-optimizations --prefix=/usr/local/python3.12 && \
    make -j$(nproc) && \
    make install

ENV PATH="/usr/local/python3.12/bin:${PATH}"

# 安装pip
RUN wget https://bootstrap.pypa.io/get-pip.py && \
    python3.12 get-pip.py && \
    rm get-pip.py

# 验证安装
RUN python3.12 --version && pip --version

# 安装 PyTorch（CUDA 版）
RUN pip install --no-cache-dir torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124

# 设置软链接
RUN ln -s /usr/local/python3.12/bin/python3 /usr/bin/python

# 设置pip默认使用国内镜像源
RUN pip config set global.index-url https://mirrors.cloud.tencent.com/pypi/simple/ && \
    pip config set install.trusted-host mirrors.cloud.tencent.com

# 设置工作目录
WORKDIR /app

COPY . .

RUN pip install -r requirements.txt

CMD ["python", "--version"]